package com.wang.web3.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author Mr.Wang
 * @version 1.0
 * @time 2017/3/28
 * @describe
 */

public class CalendarUtils {

    public static int[] getEndTime(long time) {

        Calendar calendar = Calendar.getInstance();

        long ms = time * 1000 - calendar.getTimeInMillis();

        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        int h = (int) (day * 24 + hour);

        return new int[]{h, (int) minute, (int) second};
    }

    public static String getScTime(long ms) {

//        Calendar calendar = Calendar.getInstance();

//        long ms = time * 1000 - calendar.getTimeInMillis();

        int ss = 1;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        int h = (int) (day * 24 + hour);

        return day + "天" + hour + "小时";
    }

    //time is second 毫秒
    public static int[] getTime(long time) {

//        Calendar calendar = Calendar.getInstance();

//        long ms = time * 1000 - calendar.getTimeInMillis();

        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = time / dd;
        long hour = (time - day * dd) / hh;
        long minute = (time - day * dd - hour * hh) / mi;
        long second = (time - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = time - day * dd - hour * hh - minute * mi - second * ss;

        int h = (int) (day * 24 + hour);

        return new int[]{h, (int) minute, (int) second};
    }


    public static boolean getisSameDay(long time) {

        int SECONDS_IN_DAY = 60 * 60 * 24;

        long MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY;

        Calendar calendar = Calendar.getInstance();

        long ms2 = calendar.getTimeInMillis();

        final long interval = time - ms2;

        long day1 = (time + TimeZone.getDefault().getOffset(time)) / MILLIS_IN_DAY;
        long day2 = (ms2 + TimeZone.getDefault().getOffset(ms2)) / MILLIS_IN_DAY;

        return interval < MILLIS_IN_DAY && interval > -1L * MILLIS_IN_DAY && day1 == day2;

    }

    //
    public static String isNextDay(long day) {

        String str = "";

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = new Date(day * 1000);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            switch (diffDay) {
                case 0:
                    str = "今天";
                    break;
                case 1:
                    str = "明天";
                    break;
                case -1:
                    str = "昨天";
                    break;
            }
        }
        return str;
    }

    //
    public static long getCurrentTimes() {

        Calendar calender = Calendar.getInstance();
        return calender.getTimeInMillis();
    }

    public static long getTodayZero() {
        Date date = new Date();
        long l = 24 * 60 * 60 * 1000; //每天的毫秒数
        //date.getTime()是现在的毫秒数，它 减去 当天零点到现在的毫秒数（ 现在的毫秒数%一天总的毫秒数，取余。），理论上等于零点的毫秒数，不过这个毫秒数是UTC+0时区的。
        //减8个小时的毫秒值是为了解决时区的问题。
        return (date.getTime() - (date.getTime() % l) - 8 * 60 * 60 * 1000);
    }

    public static String getCurrentTimeString() {

        Calendar calender = Calendar.getInstance();

        Date date = new Date(calender.getTimeInMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

        return sdf.format(date);
    }

    /**
     * 获取当天时间
     */
    public static String getCurrentTime() {

        Calendar calender = Calendar.getInstance();
        int year = calender.get(Calendar.YEAR);
        int month = calender.get(Calendar.MONTH) + 1;
        int day = calender.get(Calendar.DAY_OF_MONTH);
        int hour = calender.get(Calendar.HOUR);
        int am_pm = calender.get(Calendar.AM_PM);

        return year + "/" + month + "/" + day;
    }

    /**
     * 将传入的毫秒值转化为@2017/3/22 下午2时
     *
     * @param time
     * @return
     */
    public static String getStringTime(long time) {


        Date date = new Date(time * 1000);

        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        int year = calender.get(Calendar.YEAR);
        int month = calender.get(Calendar.MONTH) + 1;
        int day = calender.get(Calendar.DAY_OF_MONTH);
        int hour = calender.get(Calendar.HOUR);
        int am_pm = calender.get(Calendar.AM_PM);
        String amtimepm = "";
        if (am_pm == 0) {
            amtimepm ="上午";
        } else {
            amtimepm = "下午";
        }

        return "@" + year + "/" + month + "/" + day + " " + amtimepm + hour + "时";
    }

    public static String getStartStringTime(long time, String dataformat) {


        Date date = new Date(time * 1000);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dataformat);
        return simpleDateFormat.format(date);
    }

    /**
     * 将传入的毫秒值转化为3/22 18:00
     *
     * @param time
     * @return
     */
    public static String getStartStringTime2(long time, String dataformat) {


        Date date = new Date(time);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dataformat);
        return simpleDateFormat.format(date);
    }

    /**
     * 将传入的毫秒值转化为@2017/3/22 下午2时
     *
     * @param time
     * @return
     */
    public static String getSelectStringTime(long time) {


        Date date = new Date(time * 1000);

        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        int year = calender.get(Calendar.YEAR);
        int month = calender.get(Calendar.MONTH) + 1;
        int day = calender.get(Calendar.DAY_OF_MONTH);
        int hour = calender.get(Calendar.HOUR);
        int am_pm = calender.get(Calendar.AM_PM);
        String amtimepm = "";
        if (am_pm == 0) {
            amtimepm = "上午";
        } else {
            amtimepm = "下午";
        }

        return "@" + year + "/" + month + "/" + day;
    }

    /**
     * 将传入的毫秒值转化为3/22
     *
     * @param time
     * @return
     */
    public static String getFinishStringTime(long time) {


        Date date = new Date(time * 1000);

        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        int year = calender.get(Calendar.YEAR);
        int month = calender.get(Calendar.MONTH) + 1;
        int day = calender.get(Calendar.DAY_OF_MONTH);
        int hour = calender.get(Calendar.HOUR);
        int am_pm = calender.get(Calendar.AM_PM);
        String amtimepm = "";
        if (am_pm == 0) {
            amtimepm = "上午";
        } else {
            amtimepm = "下午";
        }

        return month + "/" + day;
    }

    public static String getbirthdayStringTime(Date time) {


        Calendar calender = Calendar.getInstance();
        calender.setTime(time);
        int year = calender.get(Calendar.YEAR);
        int month = calender.get(Calendar.MONTH) + 1;
        int day = calender.get(Calendar.DAY_OF_MONTH);
        int hour = calender.get(Calendar.HOUR);
        int am_pm = calender.get(Calendar.AM_PM);
        String amtimepm = "";

        return year + "-" + month + "-" + day;
    }

    /**
     * 日期转换成秒数
     */
    public static long getSecondsFromDate(String expireDate) {
        if (expireDate == null || expireDate.trim().equals(""))
            return 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(expireDate);
            return (long) (date.getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0L;
        }
    }

    //计算时间 1小时以内的显示“1-59分钟前”，1天以内的显示“1-23小时前”，1天以后的都显示“几月几日”（具体样式参照设计稿）
    public static String getInformationTime(long publishedTime) {

        String showString = "";

        Calendar calender = Calendar.getInstance();
        int currenttime = (int) (calender.getTimeInMillis() / 1000);

        int leadTime = (int) (currenttime - publishedTime);
        if (leadTime >= 0 && leadTime < 60 * 60) {
            showString = (leadTime / 60) + "分钟前";
        }
        if (leadTime > 60 * 60 && leadTime < 60 * 60 * 24) {
            showString = (leadTime / 60 / 60) + "小时前";
        }

        if (leadTime > 60 * 60 * 24) {

            Date date = new Date(publishedTime * 1000);
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");

            showString = sdf.format(date);

        }
        return showString;

    }

    //评论时间
    public static String getInformationContentTime(long publishedTime) {

        String showString = "";

        Calendar calender = Calendar.getInstance();
        int currenttime = (int) (calender.getTimeInMillis() / 1000);

        int leadTime = (int) (currenttime - publishedTime);
        if (leadTime >= 0 && leadTime < 60 * 60) {
            showString = (leadTime / 60) + "分钟前";
        }
        if (leadTime > 60 * 60 && leadTime < 60 * 60 * 24) {
            showString = (leadTime / 60 / 60) + "小时前";
        }

        if (leadTime > 60 * 60 * 24) {

            Date date = new Date(publishedTime * 1000);
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");

            showString = sdf.format(date);
        }
        return showString;

    }


    //站内信的列表显示时间
    public static String getMailListTime(long publishedTime) {

        String showString = "";

        Calendar calender = Calendar.getInstance();
        int currenttime = (int) (calender.getTimeInMillis() / 1000);

        int leadTime = (int) (currenttime - publishedTime);
        if (leadTime >= 0 && leadTime < 60 * 60) {
            showString = (leadTime / 60) + "分钟前";
        } else if (leadTime > 60 * 60 && leadTime < 60 * 60 * 24) {
            showString = (leadTime / 60 / 60) + "小时前";
        } else if (leadTime > 60 * 60 * 24 && leadTime < 60 * 60 * 24 * 7) {
            showString = (leadTime / 60 / 60 / 7) + "天前";
        } else if (leadTime > 60 * 60 * 24 * 7) {

            Date date = new Date(publishedTime * 1000);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

            showString = sdf.format(date);
        }
        return showString;

    }

    //问答时间显示  2天内显示今天，明天  2天后显示日期+时间
    public static String isQuestionTimeShow(long day) {

        String str = "";

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = new Date(day * 1000);
        cal.setTime(date);
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            switch (diffDay) {
                case 0:
                    str = "今天" + sdf2.format(date);
                    break;
                case 1:
                    str = "明天" + sdf2.format(date);
                    break;
                default:
                    str = sdf.format(date);
                    break;
            }
        }
        return str;
    }
}
